package com.example.musictutor.repository.facade.impl;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.musictutor.conf.Constants;
import com.example.musictutor.domain.Note;
import com.example.musictutor.domain.Song;
import com.example.musictutor.domain.SongNote;
import com.example.musictutor.factories.NoteFactory;
import com.example.musictutor.factories.SongFactory;
import com.example.musictutor.repository.DBHelper;
import com.example.musictutor.repository.facade.DAOFacade;

public class NoteDAOFacade implements DAOFacade<Note>{

	private SQLiteDatabase database;
	private DBHelper dbAdapter;
	private Context context;
	
	public NoteDAOFacade(Context context)
	{
		this.context = context;
		dbAdapter = new DBHelper(context);
	}
	
	public void open()
	{
		database = dbAdapter.getWritableDatabase();
	}

	public void close()
	{
		dbAdapter.close();
	}
	
	@Override
	public Note findEntityById(long id) {
		open();
		Cursor cursor = database.query(Constants.TABLE_NOTE, new String[] { Constants.C_ID,
				Constants.C_NAME, Constants.C_RESOURCE_ID }, Constants.C_ID + "=?",
	            new String[] { String.valueOf(id) }, null, null, null, null);
	    if (cursor != null)
	        cursor.moveToFirst();
	 
	    SongNoteDAOFacade dao = new SongNoteDAOFacade(context);

	    Note entity = NoteFactory.createNote(id, cursor.getString(1), cursor.getInt(2));
	    close();
	    return entity;
	}

	@Override
	public List<Note> getEntities() {
		open();
		List<Note> list = new ArrayList<Note>();

	    String selectQuery = "SELECT  * FROM " + Constants.TABLE_NOTE;
	    Cursor cursor = database.rawQuery(selectQuery, null);
	 
	    Note entity;
	    if (cursor.moveToFirst()) {
	        do {
	        	entity = NoteFactory.createNote(cursor.getInt(0), cursor.getString(1), cursor.getInt(2));
	            list.add(entity);
	        } while (cursor.moveToNext());
	    }
	    
	    close();
	    return list; 
	}

	@Override
	public void deleteEntity(long id) {
		open();

	    database.delete(Constants.TABLE_NOTE, Constants.C_ID + " = ?",
	            new String[] { String.valueOf(id) });
	    close();
	}

	@Override
	public void updateEntity(Note entity) {
		open();
		
		ContentValues values = new ContentValues();
	    values.put(Constants.C_NAME, entity.getName());
	    values.put(Constants.C_RESOURCE_ID, entity.getResourceId());
	 
	    database.update(Constants.TABLE_NOTE, values, Constants.C_ID + " = ?",
	            new String[] { String.valueOf(entity.getId()) });
	    close();
	}

	@Override
	public long createEntity(Note entity) {
		open();
		
		ContentValues values = new ContentValues();
	    values.put(Constants.C_NAME, entity.getName());
	    values.put(Constants.C_RESOURCE_ID, entity.getResourceId());
	    
	    long id = database.insert(Constants.TABLE_NOTE, null, values);
	    
	    close();
		return id;
	}
	
	public void deleteAll() {
		open();
		database.execSQL("delete from " + Constants.TABLE_NOTE);
	    close();
	}

	
}
